<?php
/*
 * Created by IntelliJ IDEA.
 * User: Kwesi
 * Date: 4/11/14
 * Time: 1:38 AM
 */

/*session_start();	//start session
	if(!isset($_SESSION['username'])){	//check if the user data is in the session
		header("location: login.php");	//if not redirect back to user
	}*/
?>
<html>
<head>
    <title></title>
    <link rel="stylesheet" href="style.css">
    <script src="jquery-1.11.0.js"></script>
    <script src="gen.js"></script>
    <script type = "text/javascript">
        //makes a synchronous call to the page u and return the
        //result as object
        var x;
        function syncAjax(u){
            var obj=$.ajax(
                {
                    url:u,
                    async:false
                }
            );
            return $.parseJSON(obj.responseText);

        }

        function edit(obj,id)
        {
            var r=getCommunity(id);
            if(r.result==0)
            {
                //show error message
                /*javascript*/
                //document.getElementById("divStatus").innerHTML=r.message;
                /*Jquery*/
                $("divStatus").text(r.message);
                return;
            }
            //get the data from object r and put it in the form
            $("#community_name").prop("value",r.community.community_name);
            $("#latitude").prop("value", r.community.latitude);
            $("#longitude").prop("value", r.community.longitude);
            $("#population").prop("value", r.community.population);
            $("#household").prop("value", r.community.household);
            //show the form
            //find where the user clicked and store it in x and y
            var y= event.clientY;
            var x= event.clientX/2;
            //use x and y to set the location of the form
            $("#divEdit").css("top",y);
            $("#divEdit").css("left",x);
            //display the form
            $("#divEdit").fadeIn(300);
        }

        function add(obj)
        {
            $("#community_name").prop("value", "");
            $("#latitude").prop("value", "");
            $("#longitude").prop("value", "");
            $("#population").prop("value", "");
            $("#household").prop("value", "");

            var y= event.clientY;
            var x= event.clientX/2;
            //use x and y to set the location of the form
            $("#divAdd").css("top",y);
            $("#divAdd").css("left",x);
            //display the form
            $("#divAdd").fadeIn(500);

        }

        //returns a result object for one vaccine
        function getCommunity(id)
        {
            x = id;
            var u="community_action.php?cmd=1&id="+x;
            return syncAjax(u);
        }
        //makes asynchronous call to the save page
        function save()
        {
            //complete the url
            var cn=document.getElementById("community_name").value;
            var ct=document.getElementById("latitude").value;
            var cg=document.getElementById("longitude").value;
            var cp=document.getElementById("population").value;
            var ch=document.getElementById("household").value;

            var u="community_action.php?cmd=3&id="+x+"&cn="+cn+"&ct="+ct+"&cg="
                +cg+"&cp="+cp+"&ch="+ch;
            r = syncAjax(u);


            $.getJSON(u,saveDone);
            //reload
            location.reload();
            cancel();
        }

        function delete_com(obj,id)
        {
            var r=getCommunity(id);
            // alert(r);
            if(r.result==0){
                //show error message
                return;
            }

            //get the data from the object r and put it in the form
            $("#community_id").prop("value", r.community.community_id);
            $("#comm_name").prop("value", r.community.community_name);
            $("#pop").prop("value", r.community.population);
            $("#house").prop("value", r.community.household);
            //show the form
            //find where the user clicked and store it in x and y
            var y=event.clientY;
            var x=event.clientX/2;
            //use x and y to set the location of the form
            $("#divDelete").css("top",y);
            $("#divDelete").css("left",x);
            //display the form
            $("#divDelete").fadeIn(300);
        }

        function delete_cmm()
        {

            //var cmid = document.getElementById("community_id").value;

            var u="community_action.php?cmd=5&cmid="+x;
            alert(u);
            $("#divDelete").css("background-color","006600");
            $("#divDelete").fadeOut(500); // hides any html object
            alert("Deleted");
            location.reload();

            return syncAjax(u);

        }
        function save_add()
        {
            //complete the url
            var cna=$("#c_name").val();
            var cla=$("#c_latitude").val();
            var clg=$("#c_longitude").val();
            var cpo=$("#c_population").val();
            var cho=$("#c_household").val();
            var u="community_action.php?cmd=4&cna="+cna+"&cla="+cla+"&clg="
                +clg+"&cpo="+cpo+"&cho="+cho;
            alert(u);
            //var r = syncAjax(u);

            $.getJSON(u,saveDone);
            //reload
           window.location.reload();
            cancel_add();
        }
        function saveDone(data)
        {
            alert(data);
        }
        //hides the form
        function cancel_add()
        {
            $("#divAdd").fadeOut(500);
        }
        function cancel()
        {
            //fade out the form in half a second
            $("#divEdit").fadeOut(500);

        }
        function cancel_cm()
        {
            //fade out the form in half a second
            $("#divDelete").css("background-color","006600");
            $("#divDelete").fadeOut(500); // hides any html object
        }
    </script>

</head>
<body>
<table>
    <tr>
        <td colspan="2" id="pageheader">
           <h2> Health Information System </h2>
        </td>
    </tr>
    <tr>
        <td id="mainnav">
            <div class="menuitem">location</div>
            <div class="menuitem">opd cases</div>
            <div class="menuitem">health promotion</div>
            <div class="menuitem">nutrition</div>
            <div class="menuitem">child welfare</div>
            <div class="menuitem">family planning</div>
            <div class="menuitem"><a href="logout.php">logout</a></div>
        </td>
        <td id="content">
            <div id="divPageMenu">
                <span class="menuitem" >sub districts</span>
                <span class="menuitem" >communities</span>
                <span class="menuitem" >view map</span>
                <input type="text" id="txtSearch">
                <span class="menuitem">search</span>
            </div>
            <div id="divPageMenu">
                <span class="menuitem"><a href="school_view.php" >Schools Visits</a></span> |
                <span class="menuitem"><a href="household_view.php">Households Visits</a></span> |
                <span class="menuitem"><a href="community_members_view.php">Community Members</a></span> |
                <span class="menuitem"><a href="community_list.php">Communities</a></span> |
                <span class="menuitem"><a href="service_view.php">Family Planning Service</a></span>
            </div>

            <div id="divStatus" class="status">
                status message

            </div>
            <div id="divContent">
                <b><p align = "center">COMMUNITIES</p></b>
                <span class='hotspot' onclick='add(this)'>Add Community</span>
                <table class="reportTable" width="100%" >
                    <tr class="header" >
                        <td>ID</td>
                        <td>Community </td>
                        <td>Latitude</td>
                        <td>Longitude</td>
                        <td>Population</td>
                        <td>Household</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <?php
                    include("community.php");
                    $obj=new community();


                    if(!$obj->get_all_communities()){
                        echo "error";
                        exit();
                    }
                    $name = "";
                    $obj -> search_community($name);
                    $row=$obj->fetch();
                    $row_counter=0;
                    while($row)
                    {

                        if($row_counter%2==0)
                        {
                            $style=" class='row1' ";
                        }else{
                            $style=" class='row2'  ";
                        }
                        $id=$row['community_id'];
                        echo "<tr $style >";
                        echo "<td>$id</td>";
                        echo "<td><a href='community_detail.php?id=$id'>$row[community_name]</a></td>";
                        echo "<td>$row[latitude]</td>";
                        echo "<td>$row[longitude]</td>";
                        echo "<td>$row[population]</td>";
                        echo "<td>$row[household]</td>";
                        echo "<td><span class='hotspot' onclick='edit(this,$id)'>edit</span></td>";
                        echo "<td><span class='hotspot' onclick='delete_com(this,$id)'>delete<span></td>";
                        echo "</tr>";
                        $row=$obj->fetch();
                        $row_counter++;
                    }
                    ?>
                </table>
            </div>
        </td>
    </tr>
</table>
<div id="divEdit" class="popupForm">
    <table class="tableForm" >
        <tr>
            <td class="label">Community</td>
            <td class="field"><input type="text" value="<?php echo $row['community_name'] ?>" id="community_name" ></td>
        </tr>
        <tr>
            <td class="label">Latitude</td>
            <td class="field"><input type="text" value="" id="latitude" >
            </td>
        </tr>
        <tr>
            <td class="label">Longitude</td>
            <td class="field"><input type="text" value="" id="longitude" >
            </td>
        </tr>
        <tr>
            <td class="label">Population</td>
            <td class="field"><input type="text" value="" id="population" >
            </td>
        </tr>
        <tr>
            <td class="label">Household</td>
            <td class="field"><input type="text" value="" id="household" >
            </td>
        </tr>
        <tr>
            <td class="label"></td>
            <td class="label">
                <input type="button" value="save" onclick="save()" >
                <input type="button" value="cancel" onclick="cancel()" >
            </td>
        </tr>
    </table>
</div>

<div id="divAdd" class="popupForm">
    <table class="tableForm" >
        <tr>
            <td class="label">Community</td>
            <td class="field"><input type="text" value="" id="c_name" ></td>
        </tr>
        <tr>
            <td class="label">Latitudes</td>
            <td class="field"><input type="text" value="" id="c_latitude" >
            </td>
        </tr>
        <tr>
            <td class="label">Longitude</td>
            <td class="field"><input type="text" value="" id="c_longitude" >
            </td>
        </tr>
        <tr>
            <td class="label">Population</td>
            <td class="field"><input type="text" value="" id="c_population" >
            </td>
        </tr>
        <tr>
            <td class="label">Household</td>
            <td class="field"><input type="text" value="" id="c_household" >
            </td>
        </tr>
        <tr>
            <td class="label"></td>
            <td class="label">
                <input type="button" value="save" onclick="save_add()" >
                <input type="button" value="cancel" onclick="cancel_add()" >
            </td>
        </tr>
    </table>
</div>
<div id="divDelete" class = "popupForm">

    <table class="tableForm" >
        <tr>
            <div class="label">Are you sure you want to delete?</div>
            <td class="label">Community</td>
            <td class="field"><input type="text" value="<?php echo $row['community_name'] ?>" id="comm_name" ></td>
        </tr>
        <tr>
            <td class="label">Population</td>
            <td class="field"><input type="text" value="" id="pop" >
            </td>
        </tr>
        <tr>
            <td class="label">Household</td>
            <td class="field"><input type="text" value="" id="house" >
            </td>
        </tr>
        <tr><td class="label"></td>
            <td class="field">
                <input type="button" value="Yes" onclick="delete_cmm()" >
                <input type="button" value="No" onclick="cancel_cm()" >
            </td>
        </tr>
        <input type="hidden" value="<?php echo $row['community_id'] ?>" id ="community_id" >

    </table>

</div>
</body>
</html>
